package com.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;
import com.excel.read.Config;

public class SyncServiceFactory {

	// 10.0.0.11
	public static String SERVER_URL = "";
	public static String CLIENT2CLIENT_URL = "";

	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(SyncServiceFactory.class);

	private static SyncService syncService = null;
	private static CCService ccService = null;

	private static HessianProxyFactory factory = null;

	static {
		factory = new HessianProxyFactory();

		try {

			Config.generateTable("c:\\table_config.xls");

			FileReader fr = new FileReader(new File("c:\\server.txt"));
			BufferedReader bfr = new BufferedReader(fr);
			String str = null;
			if ((str = bfr.readLine()) != null) {
				String aServerURL = str.trim() + "login";
				SERVER_URL = aServerURL;
				CLIENT2CLIENT_URL = str.trim() + "cc";
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static SyncService getSyncService() {
		if (syncService != null) {
			return syncService;
		}
		SyncService aSyncService = null;
		try {
			aSyncService = (SyncService) factory.create(SyncService.class,
					SERVER_URL);
			syncService = aSyncService;
		} catch (MalformedURLException e) {
			log.error(e);

		}
		return syncService;
	}

	public static CCService getCCService() {
		if (ccService != null) {
			return ccService;
		}
		CCService aCCService = null;
		try {
			aCCService = (CCService) factory.create(CCService.class,
					CLIENT2CLIENT_URL);
			ccService = aCCService;
		} catch (MalformedURLException e) {
			log.error(e);

		}
		return ccService;
	}

}
